import { _decorator, Component, Node, Vec2 } from 'cc';
import { Camp, Direction } from './Define';
import { Vec2Dir } from './Const';
const { ccclass, property } = _decorator;

@ccclass('Bullet')
export class Bullet extends Component {
    @property
    public speed = 100

    public direction: Direction = Direction.Left

    public forward: Vec2 = Vec2Dir.Left

    public bullet: Node = null

    public camp: Camp = null

    onLoad() {
        this.bullet = this.node.getChildByName('Sprite')
    }

    initWithDirection(dir: Direction) {
        this.setDirection(dir)
    }

    setDirection(dir: Direction) {
        this.direction = dir

        switch (dir) {
            case Direction.Left:
                this.bullet.angle = 0
                this.forward = Vec2Dir.Left
                break
            case Direction.Right:
                this.bullet.angle = -180
                this.forward = Vec2Dir.Right
                break
            case Direction.Up:
                this.bullet.angle = -90
                this.forward = Vec2Dir.Up
                break
            case Direction.Down:
                this.bullet.angle = 90
                this.forward = Vec2Dir.Down
                break
            default:
                break
        }
    }

    move(dt: number) {
        const dis = dt * this.speed
        const pos = this.node.position
        this.node.setPosition(this.forward.x * dis + pos.x, this.forward.y * dis + pos.y);
    }
}


